Project 2 - Test Driven Development (TDD)

Test-Driven Development is philosophy that gets the programmer to develop test for the program before ever writing the code. In this section of the project I have created Unit Test for each class of the Sudoku Game.

My Soapbox

I strongly disagree with this approach for myself because this is not the way my brain works. Maybe for a new programmer in college who is first learning to design programs, this method may be beneficial. I agree that testing is often left as an afterthought but is a critical portion of any program. At my current job, we write Unit Test after each segment (or project inside a solution) is thought to be finished. If code changes inside of a segment, then the Unit Test are updated according. This method works well for us and our customers.

SudokuGameTest Project

I added a Visual Studio Test Project inside of the current solution for the Sudoku Game. Visual Studio creates accessors that allow test to be run on another project inside of a solution, without holding to the conventional declarations for methods, variables, and properties (i.e. private, internal, protected). Below is picture of the solution.

solution.PNG

SudokuTest

The Sudoku Class is the main class for the program which handles and calls all other classes. The SudokuTest class tests each method and property of the Sudoku class. Notice the private method SignInAsPlayer() method. This method is created inside of the SudokuTest Class that gets used by most of the Unit Test inside SudokuTest. This was done to keep from retyping the same code and also (and more importantly) to keep the same sign in steps in every test. This allows the individual test to actually test what they are intended to test inside of also testing the Sign In progress every time, since the Sign In progress is tested in the SignInTest Class.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sudoku_Game;
 
namespace SudokuGameTest
{
    /// <summary>
    /// Summary description for UnitTest
    /// </summary>
    [TestClass]
    public class SudokuTest
    {
        public SudokuTest()
        {
 
        }
 
        private TestContext testContextInstance;
 
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
 
        private Sudoku SignInAsPlayer()
        {
            Sudoku sud = new Sudoku(false);
            SignIn sign = new SignIn(sud);
            sign.SignInAsPlayer("tim", "tim");
            return sud;
        }
 
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void UserIDTest()
        {
            Sudoku sud = SignInAsPlayer();
            Assert.AreEqual(sud.UserID, "1");
        }
 
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void CurrentUserPlayerTest()
        {
 
            Sudoku sud = SignInAsPlayer();
            Assert.AreEqual(sud.CurrentUser, UserStatus.Player);
        }
 
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void CurrentUserGuestTest()
        {
 
            Sudoku sud = new Sudoku(false);
            SignIn sign = new SignIn(sud);
            sign.SignInAsGuest();
            Assert.AreEqual(sud.CurrentUser, UserStatus.Guest);
        }
    }
}
 



SignInTest

The SignInTest class tests the Sign In procedure for the program which is called at the launch of the program. There is only one test in this class but inside the one test, two separate test are being perform on the two ways that a user can sign into the program.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sudoku_Game;
using System.Windows.Forms;
 
namespace SudokuGameTest
{
 
    /// <summary>
    /// Summary description for SignInTest
    /// </summary>
    [TestClass]
    public class SignInTest
    {
        public SignInTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }
 
        private TestContext testContextInstance;
 
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
 
        [TestMethod]
        public void LogInTest()
        {
            Sudoku sudoku = new Sudoku(false);
            Assert.IsNotNull(sudoku);
            SignIn signIn = new SignIn(sudoku);
            Assert.IsNotNull(signIn);
            signIn.SignInAsGuest();
            Assert.AreEqual(sudoku.CurrentUser, UserStatus.Guest);
            signIn = new SignIn(sudoku);
            Assert.IsNotNull(signIn);
            signIn.SignInAsPlayer("tim", "tim");
            Assert.AreEqual(sudoku.UserID, "1");
            Assert.AreEqual(sudoku.CurrentUser, UserStatus.Player);
        }
    }
}
 


GridTest

The GridTest is largest class of the testing because the Grid class holds the main functionality of the game once a user is signed in and picks a game to play. Since most of the Grid class' members are private the use of accessors can easily be seen inside this class. Without the Grid_Accessor that is created by Visual Studio, these test would never compile. When trying to use a private member, I use:
Grid_Accessor grid = new Grid_Accessor();
instead of the normal way:
Grid grid = new Grid();
Below is the GridTest Class code.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sudoku_Game;
 
namespace SudokuGameTest
{
 
    /// <summary>
    ///This is a test class for GridTest and is intended
    ///to contain all GridTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GridTest
    {
 
 
        private TestContext testContextInstance;
 
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
 
        /// <summary>
        ///A test for Cells
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void CellsTest()
        {
            Grid_Accessor grid = new Grid_Accessor();
            List<Cell> cells = grid.Cells;
            Assert.AreEqual(81, cells.Count);
 
        }
 
        /// <summary>
        ///A test for GetCellsInSubSquare
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void GetCellsInSubSquareTest()
        {
            Grid_Accessor grid = new Grid_Accessor();
            int subSquare = 1;
            List<Cell> sub = grid.GetCellsInSubSquare(subSquare);
            foreach (Cell cell in sub)
                Assert.AreEqual(cell.SubSquare, subSquare);
        }
 
        /// <summary>
        ///A test for GetCellsInRow
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void GetCellsInRowTest()
        {
            Grid_Accessor grid = new Grid_Accessor();
            int row = 4;
            List<Cell> actual = grid.GetCellsInRow(row);
            foreach(Cell cell in actual)
                Assert.AreEqual(cell.Row, row);
        }
 
        /// <summary>
        ///A test for GetCellsInColumn
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void GetCellsInColumnTest()
        {
            Grid_Accessor grid = new Grid_Accessor();
            int col = 3;
            List<Cell> actual = grid.GetCellsInColumn(col);
            foreach (Cell cell in actual)
                Assert.AreEqual(cell.Column, col);
        }
 
        /// <summary>
        ///A test for GetCellAt
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void GetCellAtTest()
        {
            Grid_Accessor target = new Grid_Accessor();
            int row = 7;
            int col = 8;
            Cell actual = target.GetCellAt(row, col);
            Assert.AreEqual(row, actual.Row);
            Assert.AreEqual(col, actual.Column);
        }
 
        /// <summary>
        ///A test for GetAvailableNumbers
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void GetAvailableNumbersTest()
        {
            Grid_Accessor target = new Grid_Accessor();
            //Fill in Grid
            for (int i = 0; i < 8; i++)
                target.GetCellAt(1, i).Value = i+1;
            Cell cell = target.GetCellAt(1,8);
            List<int> actual = target.GetAvailableNumbers(cell);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual(9, actual[0]);
        }
    }
}
 


CellTest

A Cell in the Sudoku Game is the most basic member of the game. It contains information on each individual cell. Again, the Cell_Accessor is used instead of just using Cell when creating an object.


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sudoku_Game;
 
namespace SudokuGameTest
{
 
 
    /// <summary>
    ///This is a test class for CellTest and is intended
    ///to contain all CellTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CellTest
    {
 
 
        private TestContext testContextInstance;
 
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
 
 
 
        /// <summary>
        ///A test for Value
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void ValueTest()
        {
            int expected = 4;
            Cell_Accessor target = new Cell_Accessor(1, 1, expected);
            int actual;
            actual = target.Value;
            Assert.AreEqual(expected, actual);
        }
 
        /// <summary>
        ///A test for SubSquare
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void SubSquareTest()
        {
            Cell_Accessor target = new Cell_Accessor(1, 1);
            int expected = 1;
            int actual;
            actual = target.SubSquare;
            Assert.AreEqual(expected, actual);
        }
 
        /// <summary>
        ///A test for Row
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void RowTest()
        {
            Cell_Accessor target = new Cell_Accessor(2, 1);
            int expected = 2;
            int actual;
            actual = target.Row;
            Assert.AreEqual(expected, actual);
        }
 
        /// <summary>
        ///A test for ReadOnly
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void ReadOnlyTest()
        {
            Cell_Accessor target = new Cell_Accessor(1, 1);
            bool expected = true;
            bool actual;
            target.readOnly = expected;
            actual = target.ReadOnly;
            Assert.AreEqual(expected, actual);
        }
 
        /// <summary>
        ///A test for Column
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Sudoku Game.exe")]
        public void ColumnTest()
        {
            Cell_Accessor target = new Cell_Accessor(1, 6);
            int expected = 6;
            int actual;
            actual = target.Column;
            Assert.AreEqual(expected, actual);
        }
 
        /// <summary>
        ///A test for calculateSubSquare
        ///</summary>
        [TestMethod()]
        [("Sudoku Game.exe")]
        public void calculateSubSquareTest()
        {
            Cell_Accessor target = new Cell_Accessor(9, 9);
            int expected = 9;
            int actual;
            actual = target.SubSquare;
            Assert.AreEqual(expected, actual);
        }
    }
}
 

Test Results

Below is a screen capture of all the Unit Tests passing.

testing.PNG